package com.ysstech.kettle.controller;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.JsonUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.JobCategory;
import com.ysstech.kettle.entity.JobDatasource;
import com.ysstech.kettle.entity.JobRelation;
import com.ysstech.kettle.entity.JobTask;
import com.ysstech.kettle.service.JobCategoryService;
import com.ysstech.kettle.service.JobDatasourceService;
import com.ysstech.kettle.service.JobRelationService;
import com.ysstech.kettle.service.JobTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-06-03
 */
@Controller
@Slf4j
@Api(tags = "采集分类管理api")
@RequestMapping("/jobCategory")
public class JobCategoryController extends BaseController {

    @Autowired
    private JobCategoryService jobCategoryService;
    @Autowired
    private JobDatasourceService jobDatasourceService;
    @Autowired
    private JobTaskService jobTaskService;
    @Autowired
    private JobRelationService jobRelationService;

    @YssPermissions(value = {"kettle:job:category"})
    @ApiOperation(value = "跳转采集分类管理页面")
    @YssLog(module = "采集分类管理", remark = "跳转采集分类管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "kettle/jobCategory";
    }

    @YssPermissions(value = {"kettle:job:category"})
    @ApiOperation(value = "获取采集分类页面初始值")
    @YssLog(module = "采集分类管理", remark = "获取采集分类管理页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取采集分类信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:category"})
    @ApiOperation(value = "获取采集分类信息")
    @YssLog(module = "采集分类管理", remark = "获取采集分类信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(JobCategory jobCategory, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取采集分类信息成功",
                jobCategoryService.queryAllCategory(jobCategory, this.getPage(page, rows)));
    }

    /**
     * 新增采集分类的方法
     *
     * @param jobCategory
     * @return
     */
    @YssPermissions(value = {"kettle:job:category:add"})
    @ApiOperation(value = "新增采集分类信息")
    @YssLog(module = "采集分类管理", remark = "新增采集分类信息")
    @RequestMapping(value = "/addCategory")
    @ResponseBody
    public RestResult addCategory(JobCategory jobCategory) throws Exception {
        jobCategory.setId(DateUtil.generateUUID());
        jobCategory.setAddTime(LocalDateTime.now());
        jobCategoryService.save(jobCategory);
        return ResultGenerator.getSuccessResult("新增采集分类信息成功！");
    }

    /**
     * 修改采集分类信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:category:edit"})
    @ApiOperation(value = "修改采集分类信息")
    @YssLog(module = "采集分类管理", remark = "修改采集分类信息")
    @RequestMapping(value = "/editCategory")
    @ResponseBody
    public RestResult editCategory(JobCategory jobCategory) throws Exception {
        if (jobCategory == null || StringUtils.isBlank(jobCategory.getId())) {
            throw new BusinessException("获取采集分类信息失败，请稍后重试！");
        }
        jobCategory.setEditTime(LocalDateTime.now());
        jobCategoryService.updateById(jobCategory);
        return ResultGenerator.getSuccessResult("修改采集分类信息成功！");
    }

    /**
     * 删除采集分类信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:category:del"})
    @ApiOperation(value = "删除采集分类信息")
    @YssLog(module = "采集分类管理", remark = "删除采集分类信息")
    @RequestMapping(value = "/delCategory")
    @ResponseBody
    public RestResult delCategory(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除采集分类信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        jobCategoryService.removeByIds(list);
        QueryWrapper<JobTask> wrapper = new QueryWrapper<>();
        wrapper.in("CATEGORY_ID", list);
        jobTaskService.remove(wrapper);
        return ResultGenerator.getSuccessResult("删除采集分类信息成功！");
    }
}
